package com.mercury.tests;
import java.util.List;

import org.hibernate.*;
import com.mercury.beans.*;

public class Test2 {
	@SuppressWarnings("unchecked")
	public static void main(String[] args){
		Session session =  HibernateUtil.currentSession();
		
		//use native SQL(1)
		Query query = session.getNamedQuery("userQuery");
		query.setInteger("age",20);
		List<User> users = query.list(); // return list
		for (User u:users){
			System.out.println(u);
		}
		//use native SQL(2)
		String sql="select * from Sample where Name=:name";
		query = session.createSQLQuery(sql).addEntity(User.class);
		query.setString("name","peter");
		User user = (User)query.uniqueResult(); //return only one
		System.out.println(user);
		
		//Read partial columns/fields
		String hql = "select name from User";
		List<String> names = session.createQuery(hql).list();
		for (String name:names){
			System.out.println(name);
		}
		
		//read parital columns/fields(2)
		hql="select name, age from User";
		List<Object[]> list = session.createQuery(hql).list();  //Object[]
		for (Object[] obj:list)
			System.out.println(obj[0]+"\t"+obj[1]);
		HibernateUtil.closeSession();
	}
}
